import requests
import re
from untils import extract_text_from_font
from lxml import etree
headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
    'Cache-Control': 'max-age=0',
    'Connection': 'keep-alive',
    # 'Cookie': 'uuid_n_v=v1; uuid=A761B880B07B11EF88DB5DE7D8F48EEE50F0412EF91F4D499EF3546FA8572B2E; _csrf=1088db7cce2badbbe2bdd45c01da1683420487ccfaa8828ce2937273ff514ddd; _lxsdk_cuid=193862f3beac8-057d2b1a3854e5-26011851-144000-193862f3bea7; _lxsdk=A761B880B07B11EF88DB5DE7D8F48EEE50F0412EF91F4D499EF3546FA8572B2E; Hm_lvt_e0bacf12e04a7bd88ddbd9c74ef2b533=1733123063; HMACCOUNT=FBBB32CB19475120; _ga=GA1.1.1575998100.1733123063; _ga_WN80P4PSY7=GS1.1.1733123062.1.1.1733123084.0.0.0; Hm_lpvt_e0bacf12e04a7bd88ddbd9c74ef2b533=1733123084; __mta=252516278.1733123069404.1733123069404.1733123084419.2; _lxsdk_s=193862f3beb-646-99c-563%7C%7C6',
    'Referer': 'https://www.maoyan.com/films',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-User': '?1',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
    'sec-ch-ua': '"Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}

params = {
    'showType': '2',
}

response = requests.get('https://www.maoyan.com/films', params=params, headers=headers)
html = response.text
woff_url = 'https:' + re.search(r',url\("(.*?\.woff)"\)', response.text).group(1)
with open('font.woff', 'wb') as f:
    f.write(requests.get(woff_url).content)
dict_font = extract_text_from_font('font.woff')
for k, v in dict_font.items():
    html = html.replace(k + ';', v)

html = etree.HTML(html)
movies = html.xpath('//dl[@class="movie-list"]//dd')
# for movie in movies:
#     img = movie.xpath('.//div[@class="movie-item film-channel"]//img[last()]/@src')
#     name = ''.join(movie.xpath('.//div[@class="channel-detail movie-item-title"]/a/text()'))
#     favorites = ''.join(movie.xpath('.//div[@class="channel-detail channel-detail-orange"]/text()'))
#     print(f'{name}  {favorites}  {img}')
for film in movies:
    name = film.xpath(".//div[@class='channel-detail movie-item-title']/a/text()")[0]
    href = 'https://www.maoyan.com'+ film.xpath(".//div[@class='channel-detail movie-item-title']/a/@href")[0]
    icon = film.xpath(".//a/img[@class='movie-hover-img']/@src")[0]
    want_to_see = film.xpath(".//div[@class='channel-detail channel-detail-orange']/span/text()")[0]

    print(f'电影名字:{name}\n电影链接:{href}\n电影海报:{icon}\n想看:{want_to_see}\n')